package com.treasure.service.collocate.service.impl;

import com.treasure.service.BaseService;
import com.treasure.service.collocate.service.GainStatisticsService;
import com.treasure.service.dto.RankingListDto;
import com.treasure.service.third.utils.TokenConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description:    排行榜统计
* @Author:         panxinwen
* @CreateDate:     2018/12/9 12:27
* @Version:        1.0
*/
@Service
public class GainStatisticsServiceImpl extends BaseService implements GainStatisticsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public Map getRankingList(Integer timeType, Integer dataType) {

        MultiValueMap<String, Object> map = new LinkedMultiValueMap();
        map.set("userId",TokenConfig.getTokenByHeader().getUserId());
        map.set("timeType",timeType);
        map.set("dataType",dataType);

        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
        ParameterizedTypeReference<List<RankingListDto>> pt = new ParameterizedTypeReference<List<RankingListDto>>() {
        };
        ParameterizedTypeReference<RankingListDto> pt1 = new ParameterizedTypeReference<RankingListDto>() {
        };
        try {
            ResponseEntity<List<RankingListDto>> resp = restTemplate.exchange(this.getGateway() + "/gainStatistics/v1/getRankingList",
                    HttpMethod.POST, formEntity, pt);

            ResponseEntity<RankingListDto> resp1 = restTemplate.exchange(this.getGateway() + "/gainStatistics/v1/getMyRanking",
                    HttpMethod.POST, formEntity, pt1);
            List<RankingListDto> list = resp.getBody();
            RankingListDto rankingListDto = resp1.getBody();

            Map<String,Object> map1 = new HashMap<>();
            map1.put("list",list);
            map1.put("mine",rankingListDto);

            return map1;
        } catch (HttpClientErrorException e) {
            logger.error("查询排行榜"+this.getGateway() + "/gainStatistics/v1/getRankingList statusCode : {} error:{} ", e.getStatusCode(), e);
        }

        return null;
    }

}
